#ifndef PCLVIEWER_H
#define PCLVIEWER_H

#include <iostream>

// Qt
#include <QMainWindow>
#include <QMutex>
#include <QButtonGroup>
#include <QSemaphore>

// Point Cloud Library
#include <pcl/point_cloud.h>
#include <pcl/point_types.h>
#include <pcl/visualization/pcl_visualizer.h>

// Visualization Toolkit (VTK)
#include <vtkRenderWindow.h>

#include <qtimer.h>
#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>

#include "pclCloud.h"
#include "myThread.h"
#include "usbThread.h"
#include "myMutex.h"
#include "usbLib.h"

typedef pcl::PointXYZRGBA PointT;
typedef pcl::PointCloud<PointT> PointCloudT;

namespace Ui
{
	class PCLViewer;
}

class PCLViewer : public QMainWindow
{
	Q_OBJECT

public:
	QTimer                 *m_pTimer;
	int                     m_angle;

public:
	QSerialPortInfo        *m_serialInfo;
	QList<QSerialPortInfo>  m_serialList;
	QSerialPort            *m_serialPort;
	bool                    m_isSerialPortOpen;
	QSerialPort::SerialPortError         m_err;
	QString                 m_currentPortName;
	bool                    m_isReceiveByHex;

public:
	usbDrv                 *m_Drv;
	int                     m_Vid;
	int                     m_Pid;
	unsigned char           m_RBuffer[100000];
	UsbThread               m_usbThread;

public:
	explicit PCLViewer (QWidget *parent = 0);
	~PCLViewer ();
	int transfer_bulk_async(int ep, char *bytes, int size, int timeout);

	usb_dev_handle *m_dev;

private:
	void connectRegsit();
	void initdefault();
	void showPicture();
	void initWidget();

public Q_SLOTS:
	void randomButtonPressed ();
	void openPcdButtonPressed();
	void rotateButtonPressed();
	void saveFileButtonPressed();
	void RGBsliderReleased ();
	void pSliderValueChanged (int value);
	void redSliderValueChanged (int value);
	void greenSliderValueChanged (int value);
	void blueSliderValueChanged(int value);
	void SpeedSliderValueChanged(int value);
	void SpeedBsliderReleased();
	void onTimer();
	void onEventRecv();
	void onRadioClickEvent();
	void onComTransmiBtnDwnEvent();

protected:
	boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer;
	PointCloudT::Ptr cloud;
	//PointCloudT::Ptr dstCloud;

	pcl::PointXYZRGBA pointXyzRgb;

	//----------------------------------------
	pclCloud *m_cloud;
	MyThread  m_myThread;
	myMutex  *m_mutex;
	myMutex  *m_Rmutex;
	QSemaphore *m_samp;
	int      m_rotateStatus;
	unsigned int m_speed;
	unsigned int m_lastSpeed;
	//----------------------------------------

	//----------------------------------------
	QRect        m_deskRect;
	//----------------------------------------

private:
	void setRotateStatus(int status);
	int getRotateStatus();

	unsigned int red;
	unsigned int green;
	unsigned int blue;

private:
	Ui::PCLViewer *ui;

public:
	void initPort();
	void openComPort(char *comName, int iBaudRate);
	void closeComPort();
	void openUsbPort();
	void closeUsbPort();

	void sendData();
	void recvData();
	void onSerialReady();
private:
	void setUsbStatus(bool devStatus);
	bool isUsbOn();
	void setComStatus(bool devStatus);
	bool isComOn();

public:
    //widget define
	QButtonGroup *m_bg;
	bool          m_isComOn;
	bool          m_isUsbOn;

	void updateRadioBtnStatus(); 
};

#endif // PCLVIEWER_H
